package cn.wch.ch934xlib.assist;

import android.hardware.usb.UsbDevice;

import java.util.HashMap;
import java.util.Locale;

import cn.wch.ch934xlib.chip.Mode;
import cn.wch.ch934xlib.exception.ChipException;
import cn.wch.ch934xlib.exception.UartLibException;
import cn.wch.ch934xlib.utils.LogUtil;

/**
 * 记录流控的状态
 * 在不断电的情况下，再次打开设置流控需要用到此功能
 * 支持多设备
 */
public class ModeAssist {
    private static volatile ModeAssist modeAssist;

    private HashMap<UsbDevice, HashMap<Integer, Mode>> deviceMap;

    public static ModeAssist getInstance() {
        if(modeAssist==null){
            synchronized (ModeAssist.class){
                modeAssist=new ModeAssist();
            }
        }
        return modeAssist;
    }

    public ModeAssist() {
        deviceMap=new HashMap<>();
    }

    public void init(UsbDevice usbDevice,int serialCount){
        if(!deviceMap.containsKey(usbDevice)){
            HashMap<Integer, Mode> map=new HashMap<>();
            for (int i = 0; i < serialCount; i++) {
                map.put(i,Mode.NORMAL);
            }
            deviceMap.put(usbDevice,map);
        }else {
            LogUtil.d("the device already has flow record");
        }

    }

    public Mode queryMode(UsbDevice usbDevice,int serialNumber)throws UartLibException{
        if(deviceMap.containsKey(usbDevice)){
            HashMap<Integer, Mode> map1 = deviceMap.get(usbDevice);
            if(map1==null || !map1.containsKey(serialNumber)){
                throw  new UartLibException("this device has no mode record about the serial");
            }
            Mode mode = map1.get(serialNumber);
            LogUtil.d(String.format(Locale.getDefault(),"串口%d当前模式：%s",serialNumber,mode.getDes()));
            return mode;
        }else {
            throw  new UartLibException("queryMode: this device has no mode record");
        }
    }

    public void setMode(UsbDevice usbDevice,int serialNumber,Mode mode) throws UartLibException {
        if(deviceMap.containsKey(usbDevice)){
            HashMap<Integer, Mode> map1 = deviceMap.get(usbDevice);
            if(map1==null || !map1.containsKey(serialNumber)){
                throw  new UartLibException("this device has no setMode record about the serial");
            }

            map1.put(serialNumber,mode);
        }else {
            throw  new UartLibException("setMode: this device has no mode record");
        }
    }

    public boolean queryFlow(UsbDevice usbDevice,int serialNumber)throws UartLibException {
        if(deviceMap.containsKey(usbDevice)){
            HashMap<Integer, Mode> map1 = deviceMap.get(usbDevice);
            if(map1==null || !map1.containsKey(serialNumber)){
                throw  new UartLibException("this device has no flow record about the serial");
            }
            boolean aBoolean = (map1.get(serialNumber)==Mode.HARDFLOW);
            LogUtil.d(String.format(Locale.getDefault(),"串口%d当前流控开关状态：%b",serialNumber,aBoolean));
            return aBoolean;
        }else {
            throw  new UartLibException("this device has no flow record");
        }
    }

    public void setFlow(UsbDevice usbDevice,int serialNumber,boolean enableFlow) throws UartLibException {
        if(deviceMap.containsKey(usbDevice)){
            HashMap<Integer, Mode> map1 = deviceMap.get(usbDevice);
            if(map1==null || !map1.containsKey(serialNumber)){
                throw  new UartLibException("this device has no flow record about the serial");
            }

            map1.put(serialNumber,enableFlow? Mode.HARDFLOW:Mode.NORMAL);
        }else {
            throw  new UartLibException("this device has no flow record");
        }
    }

    public void clear(UsbDevice usbDevice){
        deviceMap.remove(usbDevice);
    }

}
